package cn.com.xuhx.testlgs;

import cn.com.xuhx.mapper.ProductMapper;
import cn.com.xuhx.pojo.Product;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 乐观锁测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class LGSTest {

    @Autowired
    private ProductMapper productMapper;

    /**
     * 测试乐观锁
     */
    @Test
    public void testLGS(){
        //小王查询 28
        //SELECT id,name,price,version FROM T_PRODUCT WHERE id=?
        Product productXW = productMapper.selectById(1507966853404315649L);

        //小李查询 28
        //SELECT id,name,price,version FROM T_PRODUCT WHERE id=?
        Product productXL = productMapper.selectById(1507966853404315649L);

        //小王修改
        productXW.setPrice(productXW.getPrice() + 15);
        //UPDATE T_PRODUCT SET name=?, price=?, version=? WHERE id=? AND version=?
        productMapper.updateById(productXW);

        //小李修改（由于版本不对，小李没有修改成功）
        productXL.setPrice(productXL.getPrice() - 5);
        //UPDATE T_PRODUCT SET name=?, price=?, version=? WHERE id=? AND version=?
        productMapper.updateById(productXL);

        //老板查询 43
        Product productLB = productMapper.selectById(1507966853404315649L);
        System.out.println(productLB);
    }
}
